package com.egene.tw.activity.member;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

//import com.egene.app.activity.main.IPagerFragment;
//import com.egene.app.activity.main.NewsTabPagerAdapter;
import com.egene.tw.activity.main.OnTabReselectListener;
//import com.egene.app.base.BaseViewPagerAdapter;
import com.egene.tw.base.BaseViewPagerFragment;
import com.egene.tw.base.ViewPageFragmentAdapter;
import com.egene.tw.ui.PagerSlidingTabStrip;

public class MemberViewPagerFragment extends BaseViewPagerFragment implements
        OnTabReselectListener {

    @Override
    protected void onSetupTabAdapter(ViewPageFragmentAdapter adapter) {
        for(MemberTab tab : MemberTab.values()){
            adapter.addTab(tab.getTitle(),tab.name(),tab.getClz(),getBundle("1"));
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabStrip.setOnClickTabListener(new PagerSlidingTabStrip.OnClickTabListener() {
            @Override
            public void onClickTab(View tab, int index) {
                if(index == 1){
                    showList(-1);
                }
                System.out.println("position:   "+index);
            }
        });
    }

    private Bundle getBundle(int newType) {
        Bundle bundle = new Bundle();
        bundle.putInt("2", newType);
        return bundle;
    }

    @Override
    protected void setScreenPageLimit() {
        mViewPager.setOffscreenPageLimit(3);
    }

    /**
     * 基类会根据不同的catalog展示相应的数据
     *
     * @param catalog
     *            要显示的数据类别
     * @return
     */
    private Bundle getBundle(String catalog) {
        Bundle bundle = new Bundle();
        bundle.putString("3", catalog);
        return bundle;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onTabReselect() {
        try {
            int currentIndex = mViewPager.getCurrentItem();
            Fragment currentFragment = getChildFragmentManager().getFragments()
                    .get(currentIndex);
            if (currentFragment != null
                    && currentFragment instanceof OnTabReselectListener) {
                OnTabReselectListener listener = (OnTabReselectListener) currentFragment;
                listener.onTabReselect();
            }
        } catch (NullPointerException e) {
        }
    }

    /**
     * 切换到认识自己列表
     * @param id 列表类型儿童，成人，美容
     */
    public void showList(int id){
        mViewPager.setCurrentItem(1);
        KnowYourselfFragment fragment = (KnowYourselfFragment) getChildFragmentManager().getFragments().get(1);
        fragment.refresh(id);
//        fragment.fetchList();

    }

    public void goToCheckReport(){
        mViewPager.setCurrentItem(2);
    }
}
